﻿//
// Copyright (c) 2011 HyperCrunch, Inc. http://www.hypercrunch.com 
// Author: Nik Kalyani  http://www.kalyani.com (Twitter: @techbubble)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System.Collections.Generic;

namespace HyperCrunch.Druid.RuleMatchers
{
    public class RuleMatcherBase
    {
        public static bool MatchStringValue(string matchTarget, List<string> matchValueList, MatchConditionType matchCondition)
        {
            // Bad data always results in no-match
            if ((matchValueList == null) || (matchValueList.Count == 0) || (string.IsNullOrEmpty(matchTarget))) return false;
            matchTarget = matchTarget.Trim().ToLower();
            if (string.IsNullOrEmpty(matchTarget)) return false;

            // Sanitize match values list and return no-match if process yields empty list
            List<string> cleanMatchValueList = new List<string>();
            for (int m = 0; m < matchValueList.Count; m++)
            {
                matchValueList[m] = matchValueList[m].Trim().ToLower();
                if (!string.IsNullOrEmpty(matchValueList[m]))
                    cleanMatchValueList.Add(matchValueList[m]);
            }
            if (cleanMatchValueList.Count == 0) return false;


            switch (matchCondition)
            {
                case MatchConditionType.Equal:
                    return (matchTarget == cleanMatchValueList[0]);
                case MatchConditionType.NotEqual:
                    return (matchTarget != cleanMatchValueList[0]);
                case MatchConditionType.LessThan:
                case MatchConditionType.LessThanEqual:
                case MatchConditionType.GreaterThan:
                case MatchConditionType.GreaterThanEqual:
                    return false;
                case MatchConditionType.InList:
                    return cleanMatchValueList.Contains(matchTarget);
                case MatchConditionType.NotInList:
                    return !cleanMatchValueList.Contains(matchTarget);
                case MatchConditionType.StartsWith:
                    return matchTarget.ToString().StartsWith(cleanMatchValueList[0]);
                case MatchConditionType.EndsWith:
                    return matchTarget.ToString().EndsWith(cleanMatchValueList[0]);
                case MatchConditionType.Contains:
                    return matchTarget.ToString().IndexOf(cleanMatchValueList[0]) > -1;
            }

            return false;
        }

        public static bool MatchNumericValue(int matchTarget, List<int> matchValueList, MatchConditionType matchCondition)
        {
            // Bad data always results in no-match
            if ((matchValueList == null) || (matchValueList.Count == 0)) return false;

            switch (matchCondition)
            {
                case MatchConditionType.Equal:
                    return (matchTarget == matchValueList[0]);
                case MatchConditionType.NotEqual:
                    return (matchTarget != matchValueList[0]);
                case MatchConditionType.LessThan:
                    return (matchTarget < matchValueList[0]);
                case MatchConditionType.LessThanEqual:
                    return (matchTarget <= matchValueList[0]);
                case MatchConditionType.GreaterThan:
                    return (matchTarget > matchValueList[0]);
                case MatchConditionType.GreaterThanEqual:
                    return (matchTarget >= matchValueList[0]);
                case MatchConditionType.InList:
                    return matchValueList.Contains(matchTarget);
                case MatchConditionType.NotInList:
                    return !matchValueList.Contains(matchTarget);
                case MatchConditionType.StartsWith:
                    return matchTarget.ToString().StartsWith(matchValueList[0].ToString());
                case MatchConditionType.EndsWith:
                    return matchTarget.ToString().EndsWith(matchValueList[0].ToString());
                case MatchConditionType.Contains:
                    return matchTarget.ToString().IndexOf(matchValueList[0].ToString()) > -1;
            }

            return false;
        }

    }
}
